var new_type=0;
var id="0";
var word="";
PageClick = function(pageclickednumber, p, type,str,work_word) {
	var startIndex = p * (pageclickednumber - 1);
	if (type != undefined) {
		new_type = type;
	}
	if (str != undefined) {
		id = str;
	}
	if(work_word!=undefined)word=work_word;
	$.ajax( {
		type : "POST",
		url : "admin_work.do?method=findWorksByType",
		data : "startIndex=" + startIndex + "&pageSize=" + p + "&type="
				+ new_type+"&str="+id+"&word="+word,
		dataType : "json",
		success : function(data) {
			var totalCount = data.totalCount;
			var pageSize = data.pageSize;
			var currentPage = data.currentPage;
			var items = data.items;
			if (items != undefined) {
				showTable(data);
				findZbpls(totalCount, pageSize, currentPage,id);
			}else{
				$("#works").empty();
				$("#mypage").empty();
				alert("无数据信息");
			}
		},
		error : function() {
			alert("请求出错");
		}
	});
}
function findZbpls(count, p, pageclickednumber) {
	$('#mypage').mypagination(count, {
		callback : PageClick
	}, p, pageclickednumber - 1);
}
function showTable(data) {
	var items = data.items;
	$("#works").empty();
	$.each(items, function(i, n) {
		if(id!=undefined&&id!="0"&&id!='null'&&i==0){
			$("#author").text("设计师"+n.designer.name+"作品管理");
		}
		var zd=n.zd;
		var zd_url="";
		if(zd==1)zd_url='<a style="cursor: pointer" onclick=updateZdWork('+data.currentPage+','+data.pageSize+',"'+n.id+'",'+n.type+')>取消置顶</a>';
		else zd_url='<a style="cursor: pointer" onclick=updateZdWork('+data.currentPage+','+data.pageSize+',"'+n.id+'",'+n.type+')>置顶</a>';
		var n_d_url="无";
		var type=n.type;
		var _type="";
		if(type==0)_type="家装";
		if(type==1)_type="公装";
		if(n.designer!=null)n_d_url='<a style="cursor: pointer" href="admin_work.do?method=lookDesigns&str='+n.designer.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&page=lookDes&type='+new_type+'&backpage=works">'+n.designer.name+'</a>';
		var date = new Date(n.updatetime).format('yyyy-MM-dd');
		var html='<div class="picobject">'
					+'<div class="pic"><a style="cursor: pointer" href="admin_work.do?method=lookWork&str='+n.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&type='+new_type+'&backpage=works"><img src="'+n.icon+'" width="131" height="98" /></a></div>'
					+'<div class="title">'+n.workname+'<span>'+date+'</span></div>'
					+'<div class="title">设计师:<span>'+n_d_url+'</span></div>'
					+'<div class="title">'+_type+'<span>'+zd_url+'</span></div>'
					+'<div class="title STYLE2">'+n.special+'-'+n.style+'<span> <img src="admin/images/edit.gif" /> <a style="cursor: pointer;" href="admin_work.do?method=editorWork&str='+n.id+'&currentPage='+data.currentPage+'&pageSize='+data.pageSize+'&type='+new_type+'">编辑</a> <img src="admin/images/del.gif" /> <a style="cursor: pointer;" onclick=deleteWorkById('+data.currentPage+',"'+n.id+'",'+data.pageSize+','+data.totalCount+')>删除</a></span> </div>'
					+'</div>';
		$("#works").append(html);
	});
}
function deleteWorkById(currentPage,str,pageSize,totalCount){
	var pageNum=Math.ceil(totalCount/pageSize);
	var url="admin_work.do?method=deleteWork";
	$.getJSON(url,{id:str},  function(data){
		if(data==-1){
			alert("删除作品集出错");
		}else if(data==0){
			if(currentPage==pageNum&&(totalCount%pageSize==1||pageSize==1)){
				currentPage=currentPage-1;
			}
			if(currentPage==0)currentPage=1;
			PageClick(currentPage,pageSize,new_type,id,word);
			alert("删除成功");
		}
	});
}
function updateZdWork(currentPage,pageSize,str,type){
	var url="admin_work.do?method=updateZdWork";
	$.getJSON(url, { str: str}, function(data){
		if(data==-1)alert("请求出错");
		else if(data==0){
			if(type==0)
			alert("家装作品置顶已满");
			if(type==1)
				alert("公装作品置顶已满");
		}
		else {
			if(data==1)alert("置顶成功");
			if(data==2)alert("取消置顶成功");
			PageClick(currentPage,pageSize,new_type,id,word);
		}
	});
}